package com.hrms.controller.user;

import com.hrms.entity.Customer;
import com.hrms.entity.User;
import com.hrms.http.ResponseResult;
import com.hrms.service.CustomerService;
import com.hrms.service.UserService;
import com.hrms.utils.EntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;

@Transactional
@RestController
@RequestMapping("/user/center/usermessage")
public class UserMessageController {
    // 用户个人信息

    @Autowired
    private UserService userService;
    @Autowired
    private CustomerService customerService;

    @RequestMapping("/init")
    public ResponseResult<List<Map<String, Object>>> init(@RequestBody EntityUtil entity) {
        // 初始化
        User user = new User();
        user.setUser_id(entity.getUser_id());
        List<Map<String, Object>> list = userService.selectCustomer(user);

        if(list.size() != 1) {
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", list);
    }

    @RequestMapping("/updateAll")
    public ResponseResult<Object> updateAll(@RequestBody EntityUtil entity) {
        // 修改用户个人信息（首次）
        User user = new User();
        user.setUser_id(entity.getUser_id());
        user.setCustomer_id(entity.getCustomer_id());

        Customer customer = new Customer();
        customer.setCustomer_id(entity.getCustomer_id());
        customer.setCustomer_name(entity.getCustomer_name());
        customer.setCustomer_phone(entity.getCustomer_phone());
        customer.setCustomer_sex(entity.getCustomer_sex());

        // 新建用户真实信息
        if(!customerService.add(customer)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 绑定用户真实信息
        if(!userService.addCustomer(user)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }

    @RequestMapping("/updatePhone")
    public ResponseResult<Object> updatePhone(@RequestBody EntityUtil entity) {
        // 修改用户个人信息（电话号码）
        User user = new User();
        user.setUser_id(entity.getUser_id());

        Customer customer = new Customer();
        customer.setCustomer_id(userService.selectCustomerId(user));
        customer.setCustomer_phone(entity.getCustomer_phone());

        if(!customerService.updatePhone(customer)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }
}
